package com.thanks.thinkchina.config;

import com.thanks.thinkchina.constant.CommonConstants;
import com.thanks.thinkchina.modules.entity.LiveRate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;


@Configuration
public class ForeignExchangeRestClient {
	
	private final String API_ACCESS_KEY = "c78d1728a68ff4e023217d10f6a3dd3e";

	@Autowired
	@Qualifier(value = "remoteRestTemplate")
	private RestTemplate restTemplate;

	@Bean
	public LiveRate liveRate() {
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(CommonConstants.LIVE_API_FIXER)
				.queryParam("access_key", API_ACCESS_KEY);
		return restTemplate.getForObject(builder.toUriString(), LiveRate.class);
	}

	@Bean
	public LiveRate symbolsRate(String symbols) {
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(CommonConstants.SYMBOLS_API_FIXER)
				.queryParam("access_key", API_ACCESS_KEY)
				.queryParam("symbols", symbols);

		return restTemplate.getForObject(builder.toUriString(), LiveRate.class);
	}
}
